package org.example.myleet.p1711;

import org.example.myleet.Utils.Assert;

import java.io.*;

public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        Assert.isTrue(0 == solution.countPairs(new int[]{1}));
        Assert.isTrue(1 == solution.countPairs(new int[]{1,0}));
        Assert.isTrue(4 == solution.countPairs(new int[]{1,3,5,7,9}));
        Assert.isTrue(15 == solution.countPairs(new int[]{1,1,1,3,3,3,7}));
        Assert.isTrue(23 == solution.countPairs(new int[]{1,1,1,3,3,3,3,7,9,5}));
        Assert.isTrue(528 == solution.countPairs(new int[]{64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64}));
        String path = solution.getClass().getResource("").getPath() + "input1.txt";
        Assert.isTrue(565285169 == solution.countPairs(readFromFile(path)));
        Assert.isTrue(12 == solution.countPairs(new int[]{149,107,1,63,0,1,6867,1325,5611,2581,39,89,46,18,12,20,22,234}));
    }

    private static int[] readFromFile(String filePath) {
        BufferedReader reader = null;
        try {
            File f = new File(filePath);
            reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream(f))));
            String line = reader.readLine();
            String[] nums = line.split(",");
            int i = 0;
            int[] result = new int[nums.length];
            for (String num : nums) {
                result[i++] = Integer.parseInt(num);
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (null != reader) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
